#!/bin/sh
# anaconda-diskroot: find our root image on the given disk device
# usage: anaconda-diskroot DEVICE [PATH]

. /lib/anaconda-lib.sh
command -v getargbool >/dev/null || . /lib/dracut-lib.sh

# Run checkisomd5 on a device
run_checkisomd5() {
    livedev=$1
    if getargbool 0 rd.live.check -d check; then
        [ -b "$livedev" ] && fs=$(blkid -s TYPE -o value "$livedev")
        if [ "$fs" = "iso9660" ] || [ "$fs" = "udf" ]; then
            [ -x /bin/plymouth ] && /bin/plymouth --hide-splash
            if [ -n "$DRACUT_SYSTEMD" ]; then
                p=$(dev_unit_name "$livedev")
                systemctl start "checkisomd5@${p}.service"
                retcode=$(systemctl -p ExecMainStatus show "checkisomd5@${p}.service")
                status=$(systemctl -p ActiveState show "checkisomd5@${p}.service")
                splitsep "=" "$retcode" ignore rc
                splitsep "=" "$status" ignore state
            else
                checkisomd5 --verbose "$livedev"
                rc=$?
                state="inactive"
            fi
            if [ "$rc" = "1" ]; then
                die "CD check failed!"
                exit 1
            fi
            if [ "$state" = "failed" ]; then
                die "CD check failed!"
                exit 1
            fi
            [ -x /bin/plymouth ] && /bin/plymouth --show-splash
        fi
    fi
}

dev="$1"
path="$2" # optional, could be empty
kickstart="$(getarg inst.ks=)"

# Log the device that triggered this job.
debug_msg "Trying to find a root image on the device $dev."

# Do we already have a root device?
# Then do not run again.
[ -e "/dev/root" ] && exit 1

# Skip partitions on a disk with the iso9660 filesystem. Blivet doesn't
# recognize these partitions, so we mount the disk in stage2. However, it
# will fail if one of its partitions is already mounted, for example here.
# Therefore, skip the partitions and use the disk to find our root image.
# See the bug 1878784.
if dev_is_on_disk_with_iso9660 "$dev"; then
  debug_msg "Skipping $dev on a disk with iso9660."
  exit 1
fi

# If we're waiting for a cdrom kickstart, the user might need to swap discs.
# So if this is a CDROM drive, make a note of it, but don't mount it (yet).
# Once we get the kickstart either the udev trigger or disk-reinsertion will
# retrigger this script, and we'll mount the disk as normal.
if str_starts "$kickstart" "cdrom" && [ ! -e /tmp/ks.cfg.done ]; then
    if dev_is_cdrom "$dev"; then
        true > /tmp/anaconda-on-cdrom
        exit 0
    fi
fi

# If interactive dd has been requested and this is a cdrom, wait to mount it
# later. The user may want to swap in a driver update cdrom first.
if [ -e /tmp/dd_interactive ] && [ ! -e /tmp/dd.done ]; then
    if dev_is_cdrom "$dev"; then
        true > /tmp/anaconda-dd-on-cdrom
        exit 0
    fi
fi

info "anaconda using disk root at $dev"
mount -o ro "$dev" "$repodir" || warn "Couldn't mount $dev"
anaconda_live_root_dir "$repodir" "$path"
run_checkisomd5 "$dev"
